﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Data.OracleClient;
using System.Text;
using ReflectionStudio.Core.Events;
using INTEX.DataBase.Tools;
using INTEX.DataBase;
namespace WPFExcelReport
{

    public class JLTVV06WXData : BaseFunction
    {

        SETINTEXDB INTEXDB = new SETINTEXDB();
        INTEXDBC INTEXDBC;
        JLTWVKDOCData m_vkd = new JLTWVKDOCData();
        public JLTVV06WXData()
            : base()
        {

        }
        public JLTVV06WXData(OracleConnection _myConnection, IntexReptServBLClient _proxy, string userID = "        ")
            : base(_myConnection, _proxy, userID)
        {
            INTEXDBC = new INTEXDBC(OracleDB, proxy);
        }
        public JLTVV06WXData(Dictionary<string, object> DicGlobalPara)
            : base(DicGlobalPara)
        {
        }

        public DataSet ListVVTLIEF(DataRow dwInput)
        {
            DataSet SReturn = new DataSet();
            DataSet SResult = new DataSet();
            DataSet STemp = new DataSet();
            DataRow dwTemp;
            try
            {
                string vvliefsch = "";
                dwTemp = INTEXDB.VVTLIEF.NewRow();
                dwTemp["VVTAUFKRS"] = dwInput["VKPAUFKRS"];
                dwTemp["VVTAUFLFD"] = dwInput["VKPAUFLFD"];
                SResult = INTEXDBC.QueryVVTLIEF(dwTemp, 2);
                SReturn.Merge(SResult);
                foreach (DataRow row in SResult.Tables[0].Rows)
                {
                    dwTemp = INTEXDB.VVLIEF.NewRow();
                    dwTemp["VVMANDANT"] = row["VVTMANDANT"];
                    dwTemp["VVLIEFSCH"] = row["VVTLIEFSCH"];
                    STemp = INTEXDBC.QueryVVLIEF(dwTemp, 1);
                    SReturn.Merge(STemp);
                }
                foreach (DataRow row in STemp.Tables["VVLIEF"].Rows)
                {
                    vvliefsch = row["VVLIEFSCH"].ToString();
                    SResult = BuildData(vvliefsch, false);
                    SReturn.Merge(SResult);
                }
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
            return SReturn;
        }

        public DataSet BuildData(string vvliefsch, bool load = true)
        {
            DataSet SReturn = new DataSet();
            DataSet SResult = new DataSet();
            DataSet STemp = new DataSet();
            DataRow dwTemp;
            try
            {
                m_vkd.OracleDB = OracleDB;
                m_vkd.proxy = proxy;
                m_vkd.INTEXDBC = this.INTEXDBC;

                dwTemp = INTEXDB.VVLIEF.NewRow();
                dwTemp["VVMANDANT"] = "00";
                dwTemp["VVLIEFSCH"] = vvliefsch;
                SResult = INTEXDBC.QueryVVLIEF(dwTemp, 1);
                SReturn.Merge(SResult);
                string processVVLIEF = "";
                foreach (DataRow row in SResult.Tables["VVLIEF"].Rows)
                {
                    string vvtliefsch = row["VVLIEFSCH"].ToString();
                    if (processVVLIEF.Contains(vvtliefsch + ";"))
                        continue;
                    processVVLIEF += vvtliefsch + ";";
                    if (load)
                    {
                        dwTemp = INTEXDB.WBBST.NewRow();
                        dwTemp["WBBMANDANT"] = row["VVMANDANT"];
                        dwTemp["WBBALIEFSCH"] = row["VVLIEFSCH"];
                        STemp = INTEXDBC.QueryWBBST(dwTemp, 4);
                        /*
                        dwTemp = INTEXDB.TXTIND.NewRow();
                        dwTemp["TXTIMANDANT"] = row["VVMANDANT"];
                        dwTemp["TXTITXTART"] = "VTL";
                        dwTemp["TXTISIND"] = vvtliefsch + "PICK";
                        SResult = INTEXDBC.QueryTXTKON(dwTemp, 1);
                        STemp.Merge(SResult);*/
                        dwTemp = INTEXDB.VERSART.NewRow();

                        dwTemp["VVERSART"] = row["VVVERSART"];
                        dwTemp["VSPED"] = row["VVSPED"];
                        SResult = INTEXDBC.QueryVERSART(dwTemp, 1);
                        STemp.Merge(SResult);
                    }
                    dwTemp = INTEXDB.VVTLIEF.NewRow();
                    dwTemp["VVTMANDANT"] = row["VVMANDANT"];
                    dwTemp["VVTLIEFSCH"] = row["VVLIEFSCH"];
                    SResult = INTEXDBC.QueryVVTLIEF(dwTemp, 3);
                    SReturn.Merge(SResult);
                    SResult = INTEXDBC.QueryKUNDADR(row, 2);
                    SReturn.Merge(SResult);                    
                }
                SReturn.Merge(STemp);
                string processvk = "";
                STemp = new DataSet();
                foreach (DataRow row in SReturn.Tables["VVTLIEF"].Rows)
                {
                    string krs = row["VVTAUFKRS"].ToString();
                    string lfd = row["VVTAUFLFD"].ToString();
                    if (processvk.Contains(krs + lfd + ";"))
                        continue;
                    processvk += krs + lfd + ";";
                    dwTemp = INTEXDB.VKKOPF.NewRow();
                    dwTemp["VKKMANDANT"] = row["VVTMANDANT"];
                    dwTemp["VKKAUFKRS"] = row["VVTAUFKRS"];
                    dwTemp["VKKAUFLFD"] = row["VVTAUFLFD"];
                    SResult = m_vkd.GetData(dwTemp);
                    STemp.Merge(SResult);
                }
                SReturn.Merge(STemp);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
            return SReturn;
        }
    }
}